<template>
	<view>
		<uni-nav-bar dark :fixed="true" shadow background-color="red" status-bar left-icon="left" left-text="返回"
			title="发布券" @clickLeft="goBack"></uni-nav-bar>

		<view class="caozuo ti" v-if="i == 2">
			<view class="caozuogroupall">
				<view class="caozuogroupselect" @tap="cj(2)">现金券抽奖</view>
				<view class="caozuogroup" @tap="cj(3)">折扣券抽奖</view>
				<view class="caozuogroup" @tap="cj(4)">商品券抽奖</view>
			</view>
			<view style="height: 95%;overflow-y: auto;">
				<!-- 使用v-for循环渲染多个表单 -->
				<view v-for="(form, index) in cashForms" :key="index" class="bor">
					<view class="bor_ing">
						<view class="left">
							<!-- 红包图片 <br />（后台自带） -->
							<image v-if="form.CashCouponImgs" :src="form.CashCouponImgs" class="preview-image"></image>
							<view v-else class="upload-placeholder" @click="uploadpic('CashCouponImgs',index)">
								<text class="upload-icon">+</text>
								<text class="upload-text">上传现金券图片</text>
							</view>
						</view>
						<view class="right">
							<view class="li">
								<view class="inputSplit1">劵 金 额</view>
								<view class="inputSplit2"><input class="ins" v-model="form.CashCouponAmount" /></view>
								<view class="inputSplit3">元</view>
							</view>
							<view class="li">
								<view class="inputSplit1">红包个数</view>
								<view class="inputSplit2"><input class="ins" v-model="form.CashCouponCount" /></view>
								<view class="inputSplit3">个</view>
							</view>
							<view class="li example-body">
								<view>有 效 期</view>
								<picker mode="date" :value="form.date1" :start="startDate" :end="endDate"
									@change="(e) => bindDateChangeExpire(e, index,1)">
									<view class="uni-input">{{ form.date1 }}</view>
								</picker>
							</view>
							<view class="li example-body">
								<view>抽奖时间</view>
								<picker mode="date" :value="form.date" :start="startDate" :end="endDate"
									@change="(e) => bindDateChange(e, index,1)">
									<view class="uni-input">{{ form.date }}</view>
								</picker>
								<view>时间</view>
								<picker mode="time" :value="form.time" @change="(e) => bindTimeChange(e, index,1)">
									<view class="uni-input">{{ form.time }}</view>
								</picker>
							</view>
						</view>
					</view>

					<!-- 删除按钮 -->
					<view class="form-actions" v-if="cashForms.length > 1">
						<view class="delete-btn" @tap="removeCashForm(index)">删除</view>
					</view>
				</view>

				<!-- 添加按钮 -->
				<view class="add" @tap="addCashForm">
					+
				</view>
				<!-- 	<view class="jin">
				{{ totalCashAmount }}元
			</view> -->
				<view class="fa" @tap="releaseXJQ()">发布</view>
			</view>

		</view>

		<view class="caozuo ti" v-if="i == 3">
			<view class="caozuogroupall">
				<view class="caozuogroup" @tap="cj(2)">现金券抽奖</view>
				<view class="caozuogroupselect" @tap="cj(3)">折扣券抽奖</view>
				<view class="caozuogroup" @tap="cj(4)">商品券抽奖</view>
			</view>
			<!-- <view class="cao_title">折扣劵</view> -->
			<view style="height: 95%;overflow-y: auto;">
				<view v-for="(form, index) in discForms" :key="index" class="bor">
					<view class="bor_ing">
						<view class="left">
							<!-- 红包图片 <br />（后台自带） -->
							<image v-if="form.DiscountCouponImgs" :src="form.DiscountCouponImgs" class="preview-image">
							</image>
							<view v-else class="upload-placeholder" @click="uploadpic('DiscountCouponImgs',index)">
								<text class="upload-icon">+</text>
								<text class="upload-text">上传折扣券图片</text>
							</view>
						</view>
						<view class="right">
							<view class="li">
								<view class="inputSplit1">折 扣 率</view>
								<view class="inputSplit2"><input class="ins" v-model="form.DiscountCouponAmount" />
								</view>
								<view class="inputSplit3">折</view>
							</view>
							<view class="li">
								<view class="inputSplit1">奖券个数</view>
								<view class="inputSplit2"><input class="ins" v-model="form.DiscountCouponCount" />
								</view>
								<view class="inputSplit3">个</view>
							</view>
							<view class="li example-body">
								<view>有 效 期</view>
								<picker mode="date" :value="form.date1" :start="startDate" :end="endDate"
									@change="(e) => bindDateChangeExpire(e, index,2)">
									<view class="uni-input">{{ form.date1 }}</view>
								</picker>
							</view>
							<view class="li example-body">
								<view>抽奖时间</view>
								<picker mode="date" :value="form.date" :start="startDate" :end="endDate"
									@change="(e) => bindDateChange(e, index,2)">
									<view class="uni-input">{{ form.date }}</view>
								</picker>
								<view>时间</view>
								<picker mode="time" :value="form.time" @change="(e) => bindTimeChange(e, index,2)">
									<view class="uni-input">{{ form.time }}</view>
								</picker>
							</view>
						</view>
					</view>
					<!-- 删除按钮 -->
					<view class="form-actions" v-if="discForms.length > 1">
						<view class="delete-btn" @tap="removeDiscForm(index)">删除</view>
					</view>
				</view>

				<view class="add" @tap="addDiscForm">
					+
				</view>
				<!-- 	<view class="jin">
				{{sumMoney.ZK}}元
			</view> -->
				<view class="fa" @tap="releaseZK()">发布</view>
			</view>

		</view>

		<view class="caozuo ti" v-if="i == 4" @tap="canyu">
			<view class="caozuogroupall">
				<view class="caozuogroup" @tap="cj(2)">现金券抽奖</view>
				<view class="caozuogroup" @tap="cj(3)">折扣券抽奖</view>
				<view class="caozuogroupselect" @tap="cj(4)">商品券抽奖</view>
			</view>
			<!-- <view class="cao_title">商品劵</view> -->
			<view style="height: 95%;overflow-y: auto;">
				<view v-for="(form, index) in goodForms" :key="index" class="bor">
					<view class="bor_ing">
						<view class="left">
							<!-- 红包图片 <br />（后台自带） -->
							<image v-if="form.GiftCouponImgs" :src="form.GiftCouponImgs" class="preview-image"></image>
							<view v-else class="upload-placeholder" @click="uploadpic('GiftCouponImgs',index)">
								<text class="upload-icon">+</text>
								<text class="upload-text">上传现金券图片</text>
							</view>
						</view>
						<view class="right">
							<view class="li">
								<view class="inputSplit1">商品名称</view>
								<view class="inputSplit2"><input class="ins" v-model="form.GiftCouponName" />
								</view>
								<view class="inputSplit3"></view>
							</view>
							<view class="li">
								<view class="inputSplit1">奖券个数</view>
								<view class="inputSplit2"><input class="ins" v-model="form.GiftCouponCount" />
								</view>
								<view class="inputSplit3">个</view>
							</view>

							<view class="li example-body">
								<view>有 效 期</view>
								<picker mode="date" :value="form.date1" :start="startDate" :end="endDate"
									@change="(e) => bindDateChangeExpire(e, index,3)">
									<view class="uni-input">{{ form.date1 }}</view>
								</picker>
							</view>
							<view class="li example-body">
								<view>抽奖时间</view>
								<picker mode="date" :value="form.date" :start="startDate" :end="endDate"
									@change="(e) => bindDateChange(e, index,3)">
									<view class="uni-input">{{ form.date }}</view>
								</picker>
								<view>时间</view>
								<picker mode="time" :value="form.time" @change="(e) => bindTimeChange(e, index,3)">
									<view class="uni-input">{{ form.time }}</view>
								</picker>
							</view>
						</view>
					</view>
					<!-- 删除按钮 -->
					<view class="form-actions" v-if="goodForms.length > 1">
						<view class="delete-btn" @tap="removeGoodForm(index)">删除</view>
					</view>
				</view>
				<view class="add" @tap="addGoodForm">
					+
				</view>
				<!-- <view class="jin">
				{{sumMoney.SP}}元
			</view> -->
				<view class="fa" @tap="releaseSP()">发布</view>
			</view>

		</view>
		<tuiFoot></tuiFoot>

	</view>
</template>

<script>
	import tuiFoot from '@/components/red/foot.vue'
	import {
		LuckyDrawRedEnvelopeInsert,
		LuckyDrawCashCouponInsert,
		LuckyDrawDiscountCouponInsert,
		LuckyDrawGiftCouponInsert,
		CreateOrderByMiniProgram,
	} from "@/api/promotion";
	export default {
		components: {
			tuiFoot
		},
		data() {
			const currentDate = this.getDate({
				format: true
			})
			const currentDateExpire = this.getDateExpire({
				format: true
			})
			return {
				date: currentDate,
				time: '10:00',
				date1: currentDateExpire,
				spqtk: false,
				PromotionData: [],
				i: 2,
				isVisible: false,
				form: {
					datetimesingle: ''
				},
				promotionsId: 0,
				formRedEnvelope: {
					"promotionsId": 0,
					"redEnvelopeImgs": "",
					"redEnvelopeCount": '',
					"redEnvelopeAmount": '',
					"luckyDrawDate": ""
				},
				formRedEnvelopeQ: {
					"promotionsId": 0,
					"redEnvelopeImgs": "",
					"CashCouponCount": '',
					"CashCouponAmount": '',
					"luckyDrawDate": "",
					"ExpirationDate": ""
				},
				formRedEnvelopeZK: {
					"promotionsId": 0,
					"redEnvelopeImgs": "",
					"DiscountCouponCount": '',
					"DiscountCouponAmount": '',
					"luckyDrawDate": "",
					"ExpirationDate": ""
				},
				formRedEnvelopeSP: {
					"promotionsId": 0,
					"GiftCouponImgs": "",
					"GiftCouponCount": '',
					"GiftCouponName": '',
					"luckyDrawDate": "",
					"ExpirationDate": ""

				},
				sumMoney: {
					XJ: 0,
					XJQ: 0,
					ZK: 0,
					SP: 0
				},
				promotionsId: 0,
				cashForms: [{
					"promotionsId": 0,
					"CashCouponImgs": "",
					"CashCouponCount": '',
					"CashCouponAmount": '',
					"luckyDrawDate": "",
					"ExpirationDate": "",
					"date": currentDate,
					"date1": currentDateExpire,
					"time": '10:00',
				}],
				discForms: [{
					"promotionsId": 0,
					"DiscountCouponImgs": "",
					"DiscountCouponAmount": "",
					"DiscountCouponCount": '',
					"redEnvelopeAmount": '',
					"luckyDrawDate": "",
					"ExpirationDate": "",
					"date": currentDate,
					"date1": currentDateExpire,
					"time": '10:00',
				}],
				goodForms: [{
					"promotionsId": 0,
					"GiftCouponImgs": "",
					"GiftCouponCount": '',
					"GiftCouponName": '',
					"luckyDrawDate": "",
					"ExpirationDate": "",
					"date": currentDate,
					"date1": currentDateExpire,
					"time": '10:00',
				}],
				domain: this.$config.HTTP_REQUEST_URL
			}
		},
		computed: {
			startDate() {
				return this.getDate('start');
			},
			endDate() {
				return this.getDate('end');
			},
			totalCashAmount() {
				return this.cashForms.reduce((total, form) => {
					const amount = parseFloat(form.CashCouponAmount) || 0;
					const count = parseInt(form.CashCouponCount) || 0;
					return total + (amount * count);
				}, 0);
			}
		},
		onLoad(option) {
			this.promotionsId = parseInt(option.id);
			// this.formRedEnvelopeQ.promotionsId = parseInt(option.id);
			// this.formRedEnvelopeZK.promotionsId = parseInt(option.id);
			// this.formRedEnvelopeSP.promotionsId = parseInt(option.id);
			this.cashForms[0].promotionsId = parseInt(option.id);
			this.discForms[0].promotionsId = parseInt(option.id);
			this.goodForms[0].promotionsId = parseInt(option.id);
		},
		methods: {
			setsumXJ() {
				var redEnvelopeAmount = parseFloat(this.formRedEnvelope.redEnvelopeAmount);
				var redEnvelopeCount = parseInt(this.formRedEnvelope.redEnvelopeCount);
				this.sumMoney.XJ = redEnvelopeAmount * redEnvelopeCount;
			},
			setsumXJQ() {
				var CashCouponAmount = parseFloat(this.formRedEnvelopeQ.CashCouponAmount);
				var CashCouponCount = parseInt(this.formRedEnvelopeQ.CashCouponCount);
				this.sumMoney.XJQ = CashCouponAmount * CashCouponCount;
			},
			bindDateChange: function(e) {
				this.date = e.detail.value
			},
			bindDateChangeExpire: function(e) {
				this.date1 = e.detail.value
			},
			bindTimeChange: function(e) {
				this.time = e.detail.value
			},
			getDate(type) {
				const date = new Date();
				let year = date.getFullYear();
				let month = date.getMonth() + 1;
				let day = date.getDate() + 1;

				if (type === 'start') {
					year = year;
				} else if (type === 'end') {
					year = year + 5;
				}
				month = month > 9 ? month : '0' + month;
				day = day > 9 ? day : '0' + day;
				return `${year}-${month}-${day}`;
			},
			getDateExpire(type) {
				const date = new Date();
				let year = date.getFullYear();
				let month = date.getMonth() + 1;
				if (month == 12) {
					month = 1;
					year = year + 1;
				} else {
					month = month + 1;
				}
				let day = date.getDate() + 1;

				if (type === 'start') {
					year = year;
				} else if (type === 'end') {
					year = year + 5;
				}
				month = month > 9 ? month : '0' + month;
				day = day > 9 ? day : '0' + day;
				return `${year}-${month}-${day}`;
			},
			promotion() {
				this.i = 1
				this.isVisible = false
			},
			changeLog(e) {
				// this.single = e
				console.log('change事件:', this.form.datetimesingle);
			},
			release() {
				this.isVisible = true
				this.i = 0
			},
			releaseXJ() {
				var redEnvelopeAmount = parseFloat(this.formRedEnvelope.redEnvelopeAmount);
				var redEnvelopeCount = parseInt(this.formRedEnvelope.redEnvelopeCount);
				this.sumMoney.XJ = redEnvelopeAmount * redEnvelopeCount;
				this.formRedEnvelope.redEnvelopeAmount = redEnvelopeAmount;
				this.formRedEnvelope.redEnvelopeCount = redEnvelopeCount;
				this.formRedEnvelope.luckyDrawDate = new Date(`${this.date} ${this.time}`);

				console.log(JSON.stringify(this.formRedEnvelope));
				LuckyDrawRedEnvelopeInsert(this.formRedEnvelope)
					.then(res => {
						if (res.status == 0) {
							//this.ApplyMoney(this.sumMoney.XJ);
							return uni.showToast({
								title: res.msg
							});
						}

					})
					.catch(res => {});
			},
			releaseXJQ() {
				debugger;
				const forms = this.cashForms.map(form => ({
					promotionsId: this.promotionsId,
					cashCouponImgs: form.CashCouponImgs.replace(this.domain, ''),
					CashCouponCount: parseInt(form.CashCouponCount),
					CashCouponAmount: parseFloat(form.CashCouponAmount),
					luckyDrawDate: new Date(`${form.date} ${form.time}`),
					ExpirationDate: new Date(form.date1)
				}));
				console.log('发布多个现金券:', forms);
				LuckyDrawCashCouponInsert(forms)
					.then(res => {
						if (res.status == 0) {
							//this.ApplyMoney(this.sumMoney.XJ);
							this.cashForms = [{
								"promotionsId": this.promotionsId,
								"CashCouponImgs": "",
								"CashCouponCount": '',
								"CashCouponAmount": '',
								"luckyDrawDate": "",
								"ExpirationDate": "",
								"date": this.date,
								"date1": this.date1,
								"time": '10:00',
							}];
							return uni.showToast({
								title: res.msg
							});
						}

					})
					.catch(res => {});
				// 这里处理发布逻辑
			},
			releaseZK() {
				const forms = this.discForms.map(form => ({
					promotionsId: this.promotionsId,
					DiscountCouponImgs: form.DiscountCouponImgs.replace(this.domain, ''),
					DiscountCouponCount: parseInt(form.DiscountCouponCount),
					DiscountCouponAmount: parseFloat(form.DiscountCouponAmount),
					luckyDrawDate: new Date(`${form.date} ${form.time}`),
					ExpirationDate: new Date(form.date1)
				}));
				console.log('发布多个折扣券:', forms);
				LuckyDrawDiscountCouponInsert(forms)
					.then(res => {
						if (res.status == 0) {
							//this.ApplyMoney(this.sumMoney.XJ);
							this.discForms = [{
								"promotionsId": this.promotionsId,
								"DiscountCouponImgs": "",
								"DiscountCouponAmount": "",
								"DiscountCouponCount": '',
								"redEnvelopeAmount": '',
								"luckyDrawDate": "",
								"ExpirationDate": "",
								"date": this.date,
								"date1": this.date1,
								"time": '10:00'
							}];
							return uni.showToast({
								title: res.msg
							});
						}

					})
					.catch(res => {});

				var DiscountCouponAmount = parseFloat(this.formRedEnvelopeZK.DiscountCouponAmount);

			},
			releaseSP() {
				const forms = this.goodForms.map(form => ({
					promotionsId: this.promotionsId,
					GiftCouponImgs: form.GiftCouponImgs.replace(this.domain, ''),
					GiftCouponCount: parseInt(form.GiftCouponCount),
					GiftCouponName: form.GiftCouponName,
					luckyDrawDate: new Date(`${form.date} ${form.time}`),
					ExpirationDate: new Date(form.date1)
				}));
				console.log('发布多个商品券:', forms);
				LuckyDrawGiftCouponInsert(forms)
					.then(res => {
						if (res.status == 0) {
							//this.ApplyMoney(this.sumMoney.XJ);
							this.goodForms = [{
								"promotionsId": this.promotionsId,
								"GiftCouponImgs": "",
								"GiftCouponCount": '',
								"GiftCouponName": '',
								"luckyDrawDate": "",
								"ExpirationDate": "",
								"date": this.date,
								"date1": this.date1,
								"time": '10:00'
							}];
							return uni.showToast({
								title: res.msg
							});
						}

					})
					.catch(res => {});

				// var DiscountCouponAmount = parseFloat(this.formRedEnvelopeZK.DiscountCouponAmount);

				// var GiftCouponCount = parseInt(this.formRedEnvelopeSP.GiftCouponCount);
				// this.formRedEnvelopeSP.GiftCouponCount = GiftCouponCount;
				// this.formRedEnvelopeSP.luckyDrawDate = new Date(`${this.date} ${this.time}`);
				// this.formRedEnvelopeSP.ExpirationDate = new Date(`${this.date1}`);

				// console.log(JSON.stringify(this.formRedEnvelopeSP));
				// LuckyDrawGiftCouponInsert(this.formRedEnvelopeSP)
				// 	.then(res => {
				// 		if (res.status == 0) {
				// 			//this.ApplyMoney(this.sumMoney.XJ);
				// 			this.goodForms = [{
				// 				"promotionsId": this.promotionsId,
				// 				"GiftCouponImgs": "",
				// 				"GiftCouponCount": '',
				// 				"GiftCouponName": '',
				// 				"luckyDrawDate": "",
				// 				"ExpirationDate": "",
				// 				"date": this.date,
				// 				"date1": this.date1,
				// 				"time": '10:00'
				// 			}];
				// 			return uni.showToast({
				// 				title: res.msg
				// 			});
				// 		}

				// 	})
				// 	.catch(res => {});
			},

			ApplyMoney(money) {
				var data = {
					AppId: "wx80a16c6d4b037812",
					Description: "发布红包",
					Amount: money * 100,
					OpenId: "o199W5BMle2ELSeH13VWagQkMvvM", //换成正式的
					notifyUrl: "https://oshenghuo.cn/"
				};
				CreateOrderByMiniProgram(data)
					.then(res => {
						debugger;
						if (res != null) {
							let state = res.status;
							if (state == 0) {
								var applydata = res.data[0];
								uni.showModal({
									title: '确认支付',
									content: `确定支付${money}元吗？`,
									success: (res) => {
										if (res.confirm) {
											// Proceed with payment
											uni.requestPayment({
												// provider: 'wxpay',
												// timeStamp: String(Date.now()),
												// nonceStr: this.generateNonceStr(),
												provider: 'wxpay',
												timeStamp: applydata.timeStamp,
												nonceStr: applydata.nonceStr,
												package: applydata
													.package, // This should come from your backend
												signType: applydata.signType,
												paySign: applydata
													.paySign, // This should be generated by your backend
												success: (res) => {
													uni.showToast({
														title: '支付成功',
														icon: 'success'
													});
													// Handle successful payment
													this.handlePaymentSuccess();
												},
												fail: (err) => {
													uni.showToast({
														title: '支付失败',
														icon: 'none'
													});
													console.error('Payment failed:', err);
												}
											});
										}
									}
								});

							}

						}
					})
					.catch(res => {
						console.log(res);
					});
				// var result=CreateOrderByMiniProgram(data);

				console.log(result)

			},

			goBack() {
				uni.navigateBack({
					delta: 1 // 返回上一页面
				});
			},
			cj(i) {
				this.i = i;
				this.cashForms = [{
					"promotionsId": this.promotionsId,
					"redEnvelopeImgs": "",
					"CashCouponCount": '',
					"CashCouponAmount": '',
					"luckyDrawDate": "",
					"ExpirationDate": "",
					"date": this.date,
					"date1": this.date1,
					"time": '10:00',
				}];
				this.discForms = [{
					"promotionsId": this.promotionsId,
					"DiscountCouponImgs": "",
					"DiscountCouponAmount": "",
					"DiscountCouponCount": '',
					"redEnvelopeAmount": '',
					"luckyDrawDate": "",
					"ExpirationDate": "",
					"date": this.date,
					"date1": this.date1,
					"time": '10:00'
				}];
				this.goodForms = [{
					"promotionsId": this.promotionsId,
					"GiftCouponImgs": "",
					"GiftCouponCount": '',
					"GiftCouponName": '',
					"luckyDrawDate": "",
					"ExpirationDate": "",
					"date": this.date,
					"date1": this.date1,
					"time": '10:00'
				}];
			},
			canyu() {
				this.spqtk = !this.spqtk;
			},
			nato(url) {

				uni.navigateTo({
					url
				})
			},
			addCashForm() {
				this.cashForms.push({
					CashCouponImgs: '',
					CashCouponAmount: '',
					CashCouponCount: '',
					date: this.date,
					date1: this.date1,
					time: this.time,
					promotionsId: this.promotionsId
				});
			},
			removeCashForm(index) {
				if (this.cashForms.length > 1) {
					this.cashForms.splice(index, 1);
				}
			},
			addDiscForm() {
				this.discForms.push({
					"promotionsId": this.promotionsId,
					"DiscountCouponImgs": "",
					"DiscountCouponImgs": "",
					"DiscountCouponCount": '',
					"redEnvelopeAmount": '',
					"luckyDrawDate": "",
					"ExpirationDate": "",
					"date": this.date,
					"date1": this.date1,
					"time": this.time,
				});
			},
			removeDiscForm(index) {
				if (this.discForms.length > 1) {
					this.discForms.splice(index, 1);
				}
			},
			addGoodForm() {
				this.goodForms.push({
					"promotionsId": this.promotionsId,
					"GiftCouponImgs": "",
					"GiftCouponCount": '',
					"GiftCouponName": '',
					"luckyDrawDate": "",
					"ExpirationDate": "",
					"date": this.date,
					"date1": this.date1,
					"time": this.time,
				});
			},
			removeGoodForm(index) {
				if (this.goodForms.length > 1) {
					this.goodForms.splice(index, 1);
				}
			},
			bindDateChange(e, index, type) {
				if (type == 1) {
					this.cashForms[index].date = e.detail.value;
				} else if (type == 2) {
					this.discForms[index].date = e.detail.value;
				} else if (type == 3) {
					this.goodForms[index].date = e.detail.value;
				} else {

				}
			},
			bindDateChangeExpire(e, index, type) {
				if (type == 1) {
					this.cashForms[index].date1 = e.detail.value;
				} else if (type == 2) {
					this.discForms[index].date1 = e.detail.value;
				} else if (type == 3) {
					this.goodForms[index].date1 = e.detail.value;
				} else {

				}

			},
			bindTimeChange(e, index, type) {
				if (type == 1) {
					this.cashForms[index].time = e.detail.value;
				} else if (type == 2) {
					this.discForms[index].time = e.detail.value;
				} else if (type == 3) {
					this.goodForms[index].time = e.detail.value;
				} else {

				}

			},
			// 检查隐私协议
			checkPrivacyAuthorize() {
				return new Promise((resolve) => {
					if (this.privacyAgree) {
						resolve(true);
						return;
					}

					// 获取隐私协议状态
					wx.getPrivacySetting({
						success: (res) => {
							if (res.needAuthorization) {
								// 需要用户授权
								wx.requirePrivacyAuthorize({
									success: () => {
										this.privacyAgree = true;
										resolve(true);
									},
									fail: () => {
										uni.showModal({
											title: '提示',
											content: '需要您同意隐私协议才能使用该功能',
											showCancel: false
										});
										resolve(false);
									}
								});
							} else {
								this.privacyAgree = true;
								resolve(true);
							}
						},
						fail: () => {
							resolve(false);
						}
					});
				});
			},
			// 修改上传图片方法
			async uploadpic(key, index) {
				// 先检查隐私协议授权
				const hasPrivacyAuth = await this.checkPrivacyAuthorize();
				if (!hasPrivacyAuth) return;

				const that = this;
				uni.chooseImage({
					count: 1,
					sizeType: ['compressed'],
					sourceType: ['album', 'camera'],
					success: function(res) {
						const tempFilePath = res.tempFilePaths[0];
						uni.showLoading({
							title: '上传中...'
						});
						uni.uploadFile({
							url: that.$config.HTTP_REQUEST_URL + '/api/wx/FileUpload/UploadFile',
							filePath: tempFilePath,
							name: 'file',
							header: {
								'Authorization': uni.getStorageSync('TOKEN') || ''
							},
							success: (uploadRes) => {
								uni.hideLoading();
								try {
									const data = JSON.parse(uploadRes.data);
									if (data.status === 0) {
										var path = data.data.replace(/\\/g, '/');
										if (key == 'CashCouponImgs') {
											that.cashForms[index][key] =
												`${that.domain}${path}`;
										}

										if (key == 'DiscountCouponImgs') {
											that.discForms[index][key] =
												`${that.domain}${path}`;
										}
										if (key == 'GiftCouponImgs') {
											that.goodForms[index][key] =
												`${that.domain}${path}`;
										}
										uni.showToast({
											title: '上传成功',
											icon: 'success'
										});
									} else {
										uni.showToast({
											title: data.message || '上传失败',
											icon: 'none'
										});
									}
								} catch (e) {
									uni.showToast({
										title: '上传失败',
										icon: 'none'
									});
								}
							},
							fail: (error) => {
								uni.hideLoading();
								uni.showToast({
									title: '上传失败',
									icon: 'none'
								});
							}
						});
					},
					fail: (err) => {
						console.error('选择图片失败：', err);
						uni.showToast({
							title: '选择图片失败',
							icon: 'none'
						});
					}
				});
			},
		}
	}
</script>

<style>
	.example-body {
		/* background-color: #fff; */
		/* padding: 33%; */
	}

	.container {
		display: flex;
		flex-direction: column;
		height: 100vh;
		/* Full viewport height */
		z-index: 1001;
	}

	.san {
		width: 20upx;
		height: 30upx;
		position: absolute;
		left: 0;
	}

	.top {
		display: flex;
		width: 95%;
		margin: 0 auto;
		margin-top: 10%;
		position: relative;
		justify-content: center;
		align-items: center;
	}

	.title {
		text-align: center;
	}

	.main {
		width: 100%;
		margin: 0 auto;
		/* border: 1px solid #A5A5A5; */
		margin-top: 5%;
		padding: 3%;
		border-radius: 15upx;
		flex: 1;
		overflow-y: auto;
		padding: 20px;
		margin-bottom: 30%;
	}

	.apple {
		background-color: #FC0000;
		color: #fff;
		width: 350upx;
		margin: 0 auto;
		height: 90upx;
		display: flex;
		align-items: center;
		justify-content: center;
		border-radius: 35upx;
		margin-top: 8%;
		font-size: 65upx;
		position: fixed;
		bottom: 10%;
		left: 50%;
		/* 水平居中，先设置左移50%: ; */
		transform: translateX(-50%);
		/* 再根据元素自身宽度调整回来: ; */
		z-index: 1000;
	}

	.li {
		display: flex;
		justify-content: space-between;
		margin-bottom: 5%;
	}

	.group {
		background-color: #FC0000;
		color: #FFFFFF;
		padding: 3%;
		border-radius: 15upx;
		width: 20%;
		display: flex;
		align-items: center;
		justify-content: center;
	}

	.right {
		width: 75%;
	}

	.right_top {
		display: flex;
		justify-content: space-between;
	}

	.b {
		font-size: 20upx;
	}

	.zu {
		font-size: 20upx;
	}

	.tui {
		background-color: #FC0000;
		color: #fff;
		display: flex;
		align-items: center;
		justify-content: center;
		border-radius: 35upx;
		width: 130upx;
		font-size: 20upx;
	}

	.mi {
		color: #3D3D3D;
		font-weight: 600;
	}

	.date {
		color: #BFBEBE;
	}

	.jia {
		margin-top: 1%;
		margin-bottom: 1%;
		font-size: 22rpx;
	}

	.bg {
		position: fixed;
		width: 100%;
		height: 100%;
		background-color: #000;
		z-index: 0;
		opacity: 0.5;
		left: 0;
		top: 0;

	}

	.caozuo {
		position: fixed;
		top: 50%;
		left: 50%;
		transform: translate(-50%, -50%);
		z-index: 99;
		background-color: #fff;
		z-index: 9;
		width: 90%;
		border-radius: 20upx;
		display: flex;
		padding: 3.5% 1.5%;
		height: 80%;
		border: 1px solid #d4d0d0;
	}

	.caozuogroupall {
		width: 100%;
		display: flex;
		/* 	margin: auto; */
	}

	.caozuogroup {
		background-color: #FC0000;
		color: #FFFFFF;
		padding: 3%;
		border-radius: 15rpx;
		width: 33%;
		margin-right: 2%;
		text-align: center;

	}

	.caozuogroupselect {
		background-color: royalblue;
		color: #FFFFFF;
		padding: 3%;
		border-radius: 15rpx;
		width: 33%;
		margin-right: 2%;
		text-align: center;

	}

	.an {
		width: 25%;
		background-color: #FC0000;
		color: #FFFFFF;
		margin-right: 2.5%;
		margin-left: 2.5%;
		display: flex;
		align-items: center;
		justify-content: center;
		flex-direction: column;
		border-radius: 10upx;
		padding: 3%;
	}

	.txt {
		text-align: center;
	}

	.cao_title {
		text-align: center;
		width: 100%;
		padding-top: 5%;
	}

	.bor {
		width: 96%;
		margin: 0 auto;
		margin-top: 5%;
		border: 1px solid #A5A5A5;
		padding: 3% 1%;
		border-radius: 15upx;
	}

	.ti {
		flex-direction: column;
	}

	.add {
		border: 1px solid #A5A5A5;
		display: flex;
		align-items: center;
		justify-content: center;
		padding: 1px;
		border-radius: 25upx;
		font-size: 45upx;
		margin-top: 5%;
	}

	.bor_ing {
		display: flex;
	}

	.bor_ing .left {
		width: 26%;
		color: #3D3D3D;
		font-size: 20upx;
		border: 1px solid #A5A5A5;
		text-align: center;

		border-radius: 15upx;
		display: flex;
		align-items: center;
		flex-direction: column;
		justify-content: center;
		margin-right: 2%;
	}

	.bor_ing .right {
		width: 70%;
		display: flex;
		flex-direction: column;
		justify-content: center;
	}

	.inputSplit1 {
		width: 30%;
		display: flex;
	}

	.inputSplit2 {
		width: 60%;
		display: flex;
	}

	.inputSplit3 {
		width: 10%;
		display: flex;
	}

	.bor_ing .right .li {
		display: flex;
		font-size: 20upx;
		color: #3D3D3D;
		margin-top: 2%;
		margin-bottom: 2%;
		align-items: center;
	}

	.ins {

		border: 1px solid #A5A5A5;
		border-radius: 13upx;
	}

	.jin {
		color: #FF0000;
		font-size: 35upx;
		font-weight: 800;
		text-align: center;
		margin-top: 5%;
	}

	.fa {
		background-color: #FC0000;
		color: #FFFFFF;
		font-size: 35upx;
		font-weight: 800;
		width: 450upx;
		height: 80upx;
		display: flex;
		align-items: center;
		justify-content: center;
		border-radius: 45upx;
		margin: 0 auto;
		/* margin-top: 10%; */
	}

	.form-actions {
		display: flex;
		justify-content: flex-end;
		/* padding: 20rpx; */
	}

	.delete-btn {
		color: #FC0000;
		font-size: 28rpx;
		padding: 10rpx 20rpx;
	}

	.add {
		border: 1px solid #A5A5A5;
		display: flex;
		align-items: center;
		justify-content: center;
		padding: 10rpx;
		border-radius: 25upx;
		font-size: 45upx;
		margin: 20rpx auto;
		width: 60rpx;
		height: 60rpx;
		cursor: pointer;
	}

	.add:active {
		background-color: #f5f5f5;
	}

	picker {
		font-size: 24rpx;
	}

	.preview-image {
		width: 100%;
		height: 100%;
		object-fit: cover;
	}

	.upload-placeholder {
		width: 100%;
		height: 100%;
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: center;
		background: #f8f8f8;
	}

	.upload-icon {
		font-size: 40rpx;
		color: #999;
		margin-bottom: 8rpx;
	}

	.upload-text {
		font-size: 24rpx;
		color: #999;
		text-align: center;
	}
</style>